package com.toughsoftware.pins.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;

import com.toughsoftware.pins.R;
import com.toughsoftware.pins.data.bean.BnPin;
import com.toughsoftware.pins.fragment.FrgPinDetail;
import com.toughsoftware.pins.fragment.FrgPinList;
import com.toughsoftware.pins.fragmentactivity.ActDetail;
import com.toughsoftware.pins.listener.LstnrPinItem;
import com.toughsoftware.pins.util.Constants;

public class MainActivity extends FragmentActivity implements LstnrPinItem{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		FrgPinList frgListado
			=(FrgPinList)getSupportFragmentManager()
				.findFragmentById(R.id.frgPinList);
		
		frgListado.setLstnrPinItem(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	public void onSelectedPinItem(BnPin c) {
		boolean isDetailEnabled = 
				(getSupportFragmentManager().findFragmentById(R.id.FrgDetalle) != null);
		
		if(isDetailEnabled) {
			((FrgPinDetail)getSupportFragmentManager()
				.findFragmentById(R.id.FrgDetalle)).showDetail(c.getStrPinComment());
		}
		else {
			Intent i = new Intent(this, ActDetail.class);
			i.putExtra(Constants.EXTRA_TEXT, c.getStrPinComment());
			startActivity(i);
		}
	}
}
